/**
 * 需要修改spring的配置文件，在sessionFactory中加入 
 * <property name="entityInterceptor" ref="encryptInterceptor" />
 */
package com.bfsuolframework.core.dao.support;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.hibernate.CallbackException;
import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
import org.springframework.stereotype.Component;

import com.bfsuolframework.core.configuration.FilePropertyConfigLoader;
import com.bfsuolframework.core.utils.EncryptUtils;

@Component
public class EncryptInterceptor extends EmptyInterceptor {   
	private static final long serialVersionUID = 1L;
	
	protected final static String JAVASSIST_IDENTIFIER = "_$$_javassist";   
     
	private Map<String,String> encrypts = new HashMap<String,String>();
	
	public EncryptInterceptor(){
		try {
			Properties props = new FilePropertyConfigLoader("/config/encrypt.properties").load();
			for(String clazz:props.stringPropertyNames()){
				String methods = props.getProperty(clazz);
				if(methods != null && !methods.equals("")){
					encrypts.put(clazz, ","+methods+",");
				}
			}
		} catch (Exception e) {
			//如果读取失败，则不处理
		}
	}
    
    @Override 
    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException {   
    	for(String clazz : encrypts.keySet()){
    		if (entity.getClass().getName().equals(clazz)) {
    			for (int index = 0; index < propertyNames.length; index++) {
    				if (hasMethod(encrypts.get(clazz),propertyNames[index])) {
    					state[index] = EncryptUtils.DESEncode((String)state[index]);
    				}
    			}
    		}
    	}
        return true;   
    }   
  
    @Override 
    public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) throws CallbackException {   
    	for(String clazz : encrypts.keySet()){
    		if (entity.getClass().getName().equals(clazz)) {
    			for (int index = 0; index < propertyNames.length; index++) {
    				if (hasMethod(encrypts.get(clazz),propertyNames[index])) {
    					currentState[index] = EncryptUtils.DESEncode((String)currentState[index]);
    				}
    			}
    		}
    	}
        return true;   
    } 
    
    @Override 
    public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types){
    	for(String clazz : encrypts.keySet()){
    		if (entity.getClass().getName().equals(clazz)) {
    			for (int index = 0; index < propertyNames.length; index++) {
    				if (hasMethod(encrypts.get(clazz),propertyNames[index])) {
    					String value = (String)state[index];
        				if(value!= null){
    						state[index] = EncryptUtils.DESDecode(value);
    					}
    				}
    			}
    		}
    	}
        return true;   
    }
    
    private boolean hasMethod(String methods , String propertyName){
    	if(methods == null || propertyName == null){
    		return false;
    	}
    	String method = "," + propertyName + ",";
    	return methods.indexOf(method) > -1;
    }
    
    
}  

